home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / swags-z / sorting.swg / 0018_QUICK4.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  2KB  |  76 lines

  1. Unit qsort;
  2.  
  3. Interface
  4.  
  5. Procedure quicksort(Var s; left,right : Word);
  6.  
  7. Implementation
  8.  
  9. Procedure quicksort(Var s; left,right : Word; SortType: sType);
  10.   { On the first call left should always be = to min and right = to max }
  11.   Var
  12.     data      : DataArr Absolute s;
  13.     pivotStr,
  14.     tempStr   : String;
  15.     pivotLong,
  16.     tempLong  : LongInt
  17.     lower,
  18.     upper,
  19.     middle    : Word;
  20.  
  21.   Procedure swap(Var a,b);
  22.     Var x : DirRec Absolute a;
  23.         y : DirRec Absolute b;
  24.         t : DirRec;
  25.     begin
  26.       t := x;
  27.       x := y;
  28.       y := t;
  29.     end;
  30.  
  31.   begin
  32.     lower := left;
  33.     upper := right;
  34.     middle:= (left + right) div 2;
  35.     Case SortType of
  36.       _name: pivotStr   := data[middle].name;
  37.       _ext : pivotStr   := data[middle].ext;
  38.       _size: pivotLong  := data[middle].Lsize;
  39.       _date: pivotLong  := data[middle].Ldate;
  40.     end; { Case SortType }
  41.     Repeat
  42.       Case SortType of
  43.         _name: begin
  44.                  While data[lower].name < pivotStr do inc(lower);
  45.                  While pivotStr < data[upper].name do dec(upper);
  46.                end;
  47.         _ext : begin
  48.                  While data[lower].ext < pivotStr do inc(lower);
  49.                  While pivotStr < data[upper].ext do dec(upper);
  50.                end;
  51.         _size: begin
  52.                  While data[lower].Lsize < pivotLong do inc(lower);
  53.                  While pivotLong < data[upper].Lsize do dec(upper);
  54.                end;
  55.         _date: begin
  56.                  While data[lower].Ldate < pivotLong do inc(lower);
  57.                  While pivotLong < data[upper].Ldate do dec(upper);
  58.                end;
  59.       end; { Case SortType }
  60.       if lower <= upper then begin
  61.         swap(data[lower],data[upper]);
  62.         inc(lower);
  63.         dec(upper);
  64.        end;
  65.     Until lower > upper;
  66.     if left < upper then quicksort(data,left,upper);
  67.     if lower < right then quicksort(data,lower,right);
  68.   end; { quicksort }
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.